package com.szly.phm.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szly.phm.common.entity.dto.ra.*;
import com.szly.phm.common.entity.dto.user.LoginUserDTO;
import com.szly.phm.common.entity.po.*;
import com.szly.phm.common.entity.result.CodeStatusEnum;
import com.szly.phm.common.entity.result.PageResult;
import com.szly.phm.common.entity.result.RootResponse;
import com.szly.phm.common.entity.vo.ra.RaTreeVO;
import com.szly.phm.common.mapper.*;
import com.szly.phm.common.service.IRaService;
import com.szly.phm.common.utils.BeanCopyUtils;
import com.szly.phm.common.utils.HandleUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.baomidou.mybatisplus.core.toolkit.Wrappers.lambdaUpdate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2021-09-19
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RaServiceImpl  implements IRaService {

    private final RaServerAreaMapper  raserverAreaMapper;

    private final RaStationMapper raStationMapper;

    private final RaServerTypeMapper raServerTypeMapper;

    private final RaRadarMapper raRadarMapper;
    private final RaTreeMapper raTreeMapper;

    private final FiRadarTypeMapper fiRadarTypeMapper;

    private final RaKeyPartsTypeMapper raKeyPartsTypeMapper;

    private final RaStorageHomeMapper raStorageHomeMapper;

    private final RaInterventionTypeMapper raInterventionTypeMapper;

    private final RaSystemMapper raSystemMapper;

    private final RaExtensionMapper raExtensionMapper;

    private final RaKeyPartsModelMapper raKeyPartsModelMapper;

    private final RaKeyPartsMapper raKeyPartsMapper;

    private final RaStorageMapper raStorageMapper;

    private final FaEnsureMapper faEnsureMapper;

    private final FaEnsurePlanMapper faEnsurePlanMapper;

    private final TaTaskMapper taTaskMapper;

    private final SysUserMapper sysUserMapper;

    private final CbandRealdataMapper cbandRealdataMapper;

    private final FiFileTypeMapper fiFileTypeMapper;

    private final TimedTaskFaultResultMapper timedTaskFaultResultMapper;
    @Override
    public RootResponse createServerArea(CreateServerAreaDTO createServerAreaDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaServerArea>();
        wrapper.eq(RaServerArea::getIsSoftDelete, false);
        wrapper.eq(RaServerArea::getName,createServerAreaDTO.getName());
        if(raserverAreaMapper.selectOne(wrapper)!=null){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
        }
        var raserverArea= new RaServerArea();
        BeanCopyUtils.copy(createServerAreaDTO,raserverArea);
        raserverArea.setId(HandleUtils.UUID32());
        raserverArea.setIsSoftDelete(false);
        raserverArea.setCreator(userDTO.getUsername());
        raserverArea.setCreateDateTime(new Date());
        int i =  raserverAreaMapper.insert(raserverArea);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectserverAreas(ServerAreaPageDTO serverAreaPageDTO) {
        PageHelper.startPage(serverAreaPageDTO.getPageNum(), serverAreaPageDTO.getPageSize());
        var queryWrapper = new LambdaQueryWrapper<RaServerArea>();
        queryWrapper.eq(RaServerArea::getIsSoftDelete, false);
        queryWrapper.orderByDesc(RaServerArea::getCreateDateTime);
        if(serverAreaPageDTO.getName()!=null){
            queryWrapper.like(RaServerArea::getName, serverAreaPageDTO.getName());
        }
        var raserverAreas= raserverAreaMapper.selectList(queryWrapper);
        var pageInfo = PageInfo.of(raserverAreas);

        return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse deleteServerAreas(String id, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaStation>();
        wrapper.eq(RaStation::getIsSoftDelete, false);
        wrapper.eq(RaStation::getAreaId, id);
        if(raStationMapper.selectList(wrapper).size()>0){
           return RootResponse.error("请先删除本区域下的雷达站！！！");
        }
        // sys_user
        LambdaQueryWrapper<SysUser> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(SysUser::getIsSoftDelete,false);
        userLambdaQueryWrapper.eq(SysUser::getAreaId,id);
        if (sysUserMapper.selectList(userLambdaQueryWrapper).size()>0){
            return RootResponse.error("该区域下存在用户，不能删除！！！");
        }
        var raserverArea=  new RaServerArea();
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        raserverArea.setId(id);
        raserverArea.setIsSoftDelete(true);
        int i =  raserverAreaMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success(raserverAreaMapper.selectById(id)) : RootResponse.error();
    }

    @Override
    public RootResponse updateServerAreas(UpdateServerArea updateServerArea, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaServerArea>();
        wrapper.eq(RaServerArea::getIsSoftDelete, false);
        wrapper.eq(RaServerArea::getName,updateServerArea.getName());
        var raserverArea1=raserverAreaMapper.selectOne(wrapper);
        if(raserverArea1!=null&&!raserverArea1.getId().equals(updateServerArea.getId())){
            return RootResponse.error("名称已存在！！！");
        }
        var raserverArea=new RaServerArea();
        BeanCopyUtils.copy(updateServerArea,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =raserverAreaMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success(raserverAreaMapper.selectById(updateServerArea.getId())) : RootResponse.error();

    }

    @Override
    public RootResponse createRaStations(CreateRaStationsDTO createServerAreaDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaStation>();
        wrapper.eq(RaStation::getIsSoftDelete, false);
        wrapper.eq(RaStation::getName,createServerAreaDTO.getName());
        if(raStationMapper.selectOne(wrapper)!=null){
            return RootResponse.error("名称已存在！！！");
        }
        var raStation= new RaStation();
        BeanCopyUtils.copy(createServerAreaDTO,raStation);
        raStation.setId(HandleUtils.UUID32());
        raStation.setIsSoftDelete(false);
        raStation.setCreator(userDTO.getUsername());
        raStation.setCreateDateTime(new Date());
        int i =  raStationMapper.insert(raStation);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectRaStations(RaStationsPageDTO serverAreaPageDTO) {

        PageHelper.startPage(serverAreaPageDTO.getPageNum(), serverAreaPageDTO.getPageSize());
        var queryWrapper = new LambdaQueryWrapper<RaStation>();
        queryWrapper.eq(RaStation::getIsSoftDelete, false);
        var raStationPageVO= raStationMapper.selectRaStation(serverAreaPageDTO);
        var pageInfo = PageInfo.of(raStationPageVO);

        return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse deleteRaStations(String id, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaRadar>();
        wrapper.eq(RaRadar::getIsSoftDelete, false);
        wrapper.eq(RaRadar::getSiteId, id);
        if(raRadarMapper.selectList(wrapper).size()>0){
           return RootResponse.error("请先删除此站点雷达！！！");
        }
        var raserverArea=  new RaStation();
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        raserverArea.setId(id);
        raserverArea.setIsSoftDelete(true);
        int i =  raStationMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success(raStationMapper.selectById(id)) : RootResponse.error();
    }

    @Override
    public RootResponse updateRaStations(UpdateRaStations updateServerArea, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaStation>();
        wrapper.eq(RaStation::getIsSoftDelete, false);
        wrapper.eq(RaStation::getName,updateServerArea.getName());
       var raStation1= raStationMapper.selectOne(wrapper);
        if(raStation1!=null&&!raStation1.getId().equals(updateServerArea.getId())){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
        }
        var raStation=new RaStation();
        BeanCopyUtils.copy(updateServerArea,raStation);
        raStation.setUpdater(userDTO.getUsername());
        raStation.setUpdateDateTime(new Date());
        int i =raStationMapper.updateById(raStation);
        return i == 1 ? RootResponse.success(raStationMapper.selectById(updateServerArea.getId())) : RootResponse.error();
    }

    @Override
    public RootResponse createRaServerTypes(CreateRaServerTypesDTO createServerAreaDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaServerType>();
        wrapper.eq(RaServerType::getIsSoftDelete, false);
        wrapper.eq(RaServerType::getName,createServerAreaDTO.getName());
        if(raServerTypeMapper.selectOne(wrapper)!=null){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
        }
        var serverType= new RaServerType();
        BeanCopyUtils.copy(createServerAreaDTO,serverType);
        serverType.setId(HandleUtils.UUID32());
        serverType.setIsSoftDelete(false);
        serverType.setCreator(userDTO.getUsername());
        serverType.setCreateDateTime(new Date());
        int i =  raServerTypeMapper.insert(serverType);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectRaServerTypes(RaServerTypesPageDTO serverAreaPageDTO) {
        PageHelper.startPage(serverAreaPageDTO.getPageNum(), serverAreaPageDTO.getPageSize());
        var queryWrapper = new LambdaQueryWrapper<RaServerType>();
        queryWrapper.eq(RaServerType::getIsSoftDelete, false);
        if(serverAreaPageDTO.getName()!=null){
            queryWrapper.like(RaServerType::getName, serverAreaPageDTO.getName());
        }
        queryWrapper.orderByDesc(RaServerType::getCreateDateTime);
        var raserverAreas= raServerTypeMapper.selectList(queryWrapper);
        var pageInfo = PageInfo.of(raserverAreas);

        return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse deleteRaServerTypes(String id, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaRadar>();
        wrapper.eq(RaRadar::getIsSoftDelete, false);
        wrapper.eq(RaRadar::getServiceId, id);
        if(raRadarMapper.selectList(wrapper).size()>0){
        return     RootResponse.error("请先删除本服务类型下的雷达！！！");
        }
        var raServerType=  new RaServerType();
        raServerType.setUpdater(userDTO.getUsername());
        raServerType.setUpdateDateTime(new Date());
        raServerType.setId(id);
        raServerType.setIsSoftDelete(true);
        int i =  raServerTypeMapper.updateById(raServerType);
        return i == 1 ? RootResponse.success(raServerTypeMapper.selectById(id)) : RootResponse.error();
    }

    @Override
    public RootResponse updateRaServerTypes(UpdateRaServerTypes updateServerArea, LoginUserDTO userDTO) {

//        var raServerType=raRadarMapper.selectRaRadarName(updateServerArea.getName(),userDTO.getAreaId());
//        if(raServerType!=null&&raServerType.size()!=0&&!raServerType.get(0).getId().equals(updateServerArea.getId())){
//            return RootResponse.error("名称已存在！！！");
//        }
        var wrapper = new LambdaQueryWrapper<RaServerType>();
        wrapper.eq(RaServerType::getIsSoftDelete, false);
        wrapper.eq(RaServerType::getName,updateServerArea.getName());
        if (raServerTypeMapper.selectOne(wrapper)!=null){
            if(!raServerTypeMapper.selectOne(wrapper).getId().equals(updateServerArea.getId())){
                return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
            }
        }
        var raserverArea=new RaServerType();
        BeanCopyUtils.copy(updateServerArea,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =raServerTypeMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success(raServerTypeMapper.selectById(updateServerArea.getId())) : RootResponse.error();
    }

    @Override
    public RootResponse createRaRadars(CreateRaRadarsDTO createMajorDTO, LoginUserDTO userDTO) {
        if(raRadarMapper.selectRaRadarName(createMajorDTO.getName(),userDTO.getAreaId()).size()!=0){
            return RootResponse.error("名称已存在！！！");
        }
        var raRadar= new RaRadar();
        BeanCopyUtils.copy(createMajorDTO,raRadar);
        String raRadarId =HandleUtils.UUID32();
        raRadar.setId(raRadarId);
        raRadar.setIsSoftDelete(false);
        raRadar.setCreator(userDTO.getUsername());
        raRadar.setCreateDateTime(new Date());
        var raTree= new RaTree();
        raTree.setId(HandleUtils.UUID32());
        raTree.setIsSoftDelete(false);
        raTree.setCreator(userDTO.getUsername());
        raTree.setCreateDateTime(new Date());
        raTree.setRadarId(raRadar.getId());
        int j =raTreeMapper.insert(raTree);
        int i =  raRadarMapper.insert(raRadar);
        int n=i+j;
        return n == 2 ?RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectRaRadars(RaRadarsPageDTO raRadarsPageDTO) {

        PageHelper.startPage(raRadarsPageDTO.getPageNum(), raRadarsPageDTO.getPageSize());

        var raRadarPageVOS=  raRadarMapper.selectRaRadar(raRadarsPageDTO);
        var pageInfo = PageInfo.of(raRadarPageVOS);

        return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse deleteRaRadars(String id, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaTree>();
        wrapper.eq(RaTree::getIsSoftDelete, false);
        wrapper.eq(RaTree::getRadarId, id);
        if(raTreeMapper.selectList(wrapper).size()>1){
            return RootResponse.error("请先删除雷达树！！！");
        }
        var wrapper4 = new LambdaQueryWrapper<RaInterventionType>();
        wrapper4.eq(RaInterventionType::getIsSoftDelete, false);
        wrapper4.eq(RaInterventionType::getRadarId, id);
        if(raInterventionTypeMapper.selectList(wrapper4).size()>0){
            return  RootResponse.error("请先删除雷达的接入方式！！！");
        }
//        var wrapper1 = new LambdaQueryWrapper<RaKeyParts>();
//        wrapper1.eq(RaKeyParts::getIsSoftDelete, false);
//        wrapper1.eq(RaKeyParts::getRadarId, id);
//        if(raKeyPartsMapper.selectList(wrapper1).size()>0){
//            return  RootResponse.error("请先删除此雷达下的关重件信息！！！");
//        }
        var wrapper2 = new LambdaQueryWrapper<TaTask>();
        wrapper2.eq(TaTask::getIsSoftDelete, false);
//        wrapper2.eq(TaTask::getTreeId, raTreeMapper.selectById().getId());
        List<TaTask> taTasks = taTaskMapper.selectList(wrapper2);
        for (TaTask taTask : taTasks) {
            var wrapper3 = new LambdaQueryWrapper<RaTree>();
            wrapper3.eq(RaTree::getIsSoftDelete, false);
            wrapper3.eq(RaTree::getId, taTask.getTreeId());
            for (RaTree raTree : raTreeMapper.selectList(wrapper3)) {
                if (raTree.getRadarId().equals(id)){
                    RootResponse.error("此雷达已被任务信息绑定！！！");
                }
            }
        }
        var raRadar=  new RaRadar();
        raRadar.setUpdater(userDTO.getUsername());
        raRadar.setUpdateDateTime(new Date());
        raRadar.setId(id);
        raRadar.setIsSoftDelete(true);
        int i =  raRadarMapper.updateById(raRadar);
        return i == 1 ? RootResponse.success(raRadarMapper.selectById(id)) : RootResponse.error();
    }

    @Override
    public RootResponse updateRadars(UpdateRaRadarDTO updateServerArea, LoginUserDTO userDTO) {
        var raRadarNames=raRadarMapper.selectRaRadarName(updateServerArea.getName(),userDTO.getAreaId());
        if(raRadarNames.size()!=0&&!raRadarNames.get(0).getId().equals(updateServerArea.getId())){
            return RootResponse.error("名称已存在！！！");
        }
        var raRadar=new RaRadar();
        BeanCopyUtils.copy(updateServerArea,raRadar);
        raRadar.setUpdater(userDTO.getUsername());
        raRadar.setUpdateDateTime(new Date());
        int i =raRadarMapper.updateById(raRadar);
        return i == 1 ? RootResponse.success(raRadarMapper.selectById(updateServerArea.getId())) : RootResponse.error();
    }

    @Override
    public RootResponse createRaInterventionTypes(CreateRaInterventionTypeDTO createRaInterventionTypeDTO, LoginUserDTO userDTO) {
        String strDateFormat = "yyyyMMddHHmmss";
        SimpleDateFormat df = new SimpleDateFormat(strDateFormat);
        df.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        var wrapper = new LambdaQueryWrapper<RaInterventionType>();
        wrapper.eq(RaInterventionType::getIsSoftDelete, false);
        wrapper.eq(RaInterventionType::getInterventionType,createRaInterventionTypeDTO.getInterventionType());
        wrapper.eq(RaInterventionType::getRadarId, createRaInterventionTypeDTO.getRadarId());
        var raRadar1= raInterventionTypeMapper.selectOne(wrapper);
        if(raRadar1!=null){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"雷达接入方式已存在！！！");
        }
        var fiRadarType= new RaInterventionType();
        BeanCopyUtils.copy(createRaInterventionTypeDTO,fiRadarType);
        fiRadarType.setId(HandleUtils.UUID32());
        fiRadarType.setIsSoftDelete(false);
        fiRadarType.setCreator(userDTO.getUsername());
        fiRadarType.setCreateDateTime(new Date());
        //开始建表
        if(createRaInterventionTypeDTO.getTypeName().contains("C")){
            int j=raInterventionTypeMapper.existTable(createRaInterventionTypeDTO.getRadarName()+"_cband_data");
            if(j==0){
               raInterventionTypeMapper.createCTable(createRaInterventionTypeDTO.getRadarName()+"_cband_data");
                    Map<String,Object> map2=cbandRealdataMapper.selectCRealData(1);
                    map2.put("uptime",df.format(new Date()));
                    map2.put("uuid", HandleUtils.UUID32());
                    map2.put("Site_Code",createRaInterventionTypeDTO.getRadarId());
                    cbandRealdataMapper.addFalseData(map2,createRaInterventionTypeDTO.getRadarName()+"_cband_data");
            }
            var wrapper2 = new LambdaQueryWrapper<RaRadar>();
            wrapper2.eq(RaRadar::getId,createRaInterventionTypeDTO.getRadarId());
            wrapper2.eq(RaRadar::getIsSoftDelete,0);
            var raRadar2= raRadarMapper.selectOne(wrapper2);
            var raRadar= new RaRadar();
            BeanCopyUtils.copy(raRadar2,raRadar);
            raRadar.setDataTableName(createRaInterventionTypeDTO.getRadarName()+"_cband_data");
            raRadarMapper.updateById(raRadar);

        }
        if(createRaInterventionTypeDTO.getTypeName().contains("S")){
            if(raInterventionTypeMapper.existTable(createRaInterventionTypeDTO.getRadarName()+"_sband_data")==0){
              raInterventionTypeMapper.createSTable(createRaInterventionTypeDTO.getRadarName()+"_sband_data");
                    Map<String,Object> map2=cbandRealdataMapper.selectSRealData(1);
                    map2.put("uptime",df.format(new Date()));
                    map2.put("uuid", HandleUtils.UUID32());
                map2.put("Site_Code",createRaInterventionTypeDTO.getRadarId());
                    cbandRealdataMapper.addFalseData(map2,createRaInterventionTypeDTO.getRadarName()+"_sband_data");
            }
            var wrapper2 = new LambdaQueryWrapper<RaRadar>();
            wrapper2.eq(RaRadar::getId,createRaInterventionTypeDTO.getRadarId());
            wrapper2.eq(RaRadar::getIsSoftDelete,0);
            var raRadar2= raRadarMapper.selectOne(wrapper2);
            var raRadar= new RaRadar();
            BeanCopyUtils.copy(raRadar2,raRadar);
            raRadar.setDataTableName(createRaInterventionTypeDTO.getRadarName()+"_sband_data");
            raRadarMapper.updateById(raRadar);
        }
        int i =  raInterventionTypeMapper.insert(fiRadarType);
        if(fiRadarType.getIsIntervention()==1){
          var raInterventionType=  new RaInterventionType();
            raInterventionType.setIsIntervention(0);
            var wrapper1 = new LambdaQueryWrapper<RaInterventionType>();
            wrapper1.eq(RaInterventionType::getIsSoftDelete, false);
            wrapper1.eq(RaInterventionType::getRadarId, createRaInterventionTypeDTO.getRadarId());
            wrapper1.ne(RaInterventionType::getId,fiRadarType.getId());
            raInterventionTypeMapper.update(raInterventionType,wrapper1);
        }
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectRaInterventionTypes(RaInterventionTypePageDTO raRadarsPageDTO) {
        PageHelper.startPage(raRadarsPageDTO.getPageNum(), raRadarsPageDTO.getPageSize());
        var queryWrapper = new LambdaQueryWrapper<RaInterventionType>();
        queryWrapper.eq(RaInterventionType::getIsSoftDelete, false);
        if(raRadarsPageDTO.getRadarId()!=null){
            queryWrapper.like(RaInterventionType::getRadarId, raRadarsPageDTO.getRadarId());
        }
        queryWrapper.orderByDesc(RaInterventionType::getCreateDateTime);
        var raserverAreas= raInterventionTypeMapper.selectList(queryWrapper);
        var pageInfo = PageInfo.of(raserverAreas);
        return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));
    }

    @Override
    public RootResponse deleteRaInterventionTypes(String id,String radarId, LoginUserDTO userDTO) {
        var raInterventionType=  new RaInterventionType();
        raInterventionType.setUpdater(userDTO.getUsername());
        raInterventionType.setUpdateDateTime(new Date());
        raInterventionType.setId(id);
        raInterventionType.setIsSoftDelete(true);
        int i =  raInterventionTypeMapper.updateById(raInterventionType);
        UpdateWrapper<RaRadar> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("data_table_name", null);
        updateWrapper.eq("id", radarId);
        raRadarMapper.update(null, updateWrapper);
        return i == 1 ? RootResponse.success(raInterventionTypeMapper.selectById(id)) : RootResponse.error();
    }

    @Override
    public RootResponse updateRaInterventionTypes(UpdateRaInterventionTypeDTO updateRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaInterventionType>();
        wrapper.eq(RaInterventionType::getIsSoftDelete, false);
        wrapper.eq(RaInterventionType::getInterventionType,updateRaInterventionTypeDTO.getInterventionType());
        wrapper.eq(RaInterventionType::getRadarId, updateRaInterventionTypeDTO.getRadarId());
        var raRadar1= raInterventionTypeMapper.selectOne(wrapper);
        if(raRadar1!=null&&!raRadar1.getId().equals(updateRaInterventionTypeDTO.getId())){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"雷达接入方式已存在！！！");
        }
        var raserverArea=new RaInterventionType();
        BeanCopyUtils.copy(updateRaInterventionTypeDTO,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =raInterventionTypeMapper.updateById(raserverArea);
        if(updateRaInterventionTypeDTO.getIsIntervention()==1){
            var raInterventionType=  new RaInterventionType();
            raInterventionType.setIsIntervention(0);
            var wrapper1 = new LambdaQueryWrapper<RaInterventionType>();
            wrapper1.eq(RaInterventionType::getIsSoftDelete, false);
            wrapper1.eq(RaInterventionType::getRadarId, updateRaInterventionTypeDTO.getRadarId());
            wrapper1.ne(RaInterventionType::getId,updateRaInterventionTypeDTO.getId());
            raInterventionTypeMapper.update(raInterventionType,wrapper1);
            if(updateRaInterventionTypeDTO.getTypeName().contains("C")){
                var wrapper2 = new LambdaQueryWrapper<RaRadar>();
                wrapper2.eq(RaRadar::getId,updateRaInterventionTypeDTO.getRadarId());
                wrapper2.eq(RaRadar::getIsSoftDelete,0);
               var raRadar2= raRadarMapper.selectOne(wrapper2);
                var raRadar= new RaRadar();
                BeanCopyUtils.copy(raRadar2,raRadar);
                raRadar.setDataTableName(updateRaInterventionTypeDTO.getRadarName()+"_sband_data");
                raRadarMapper.updateById(raRadar);
        }
        }
        return i == 1 ? RootResponse.success(raInterventionTypeMapper.selectById(updateRaInterventionTypeDTO.getId())) : RootResponse.error();
    }

    @Override
    public RootResponse createRaSystems(CreateRaSystemsDTO createRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaSystem>();
        wrapper.eq(RaSystem::getIsSoftDelete, false);
        wrapper.eq(RaSystem::getName,createRaInterventionTypeDTO.getName());
        if(raSystemMapper.selectOne(wrapper)!=null){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
        }
        var fiRadarType= new RaSystem();
        BeanCopyUtils.copy(createRaInterventionTypeDTO,fiRadarType);
        fiRadarType.setId(HandleUtils.UUID32());
        fiRadarType.setIsSoftDelete(false);
        fiRadarType.setCreator(userDTO.getUsername());
        fiRadarType.setCreateDateTime(new Date());
        int i =  raSystemMapper.insert(fiRadarType);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectRaSystems(RaSystemsPageDTO raRadarsPageDTO) {
        var ids= new HashSet<String>();
        ids.add("0");
        var queryWrapper = new LambdaQueryWrapper<RaSystem>();
        if(raRadarsPageDTO.getRadarId()!=null){
            var queryWrapper1 = new LambdaQueryWrapper<RaTree>();
            queryWrapper1.eq(RaTree::getIsSoftDelete, false);
            queryWrapper1.isNotNull(RaTree::getSystemId);
            queryWrapper1.eq(RaTree::getRadarId,raRadarsPageDTO.getRadarId());
            raTreeMapper.selectList(queryWrapper1).forEach(t->{
                ids.add(t.getSystemId());
            });
            if(ids!=null&&ids.size()!=0)
            queryWrapper.in(RaSystem::getId,ids);
        }
        PageHelper.startPage(raRadarsPageDTO.getPageNum(), raRadarsPageDTO.getPageSize());
        queryWrapper.eq(RaSystem::getIsSoftDelete, false);
        if(raRadarsPageDTO.getName()!=null){
            queryWrapper.like(RaSystem::getName, raRadarsPageDTO.getName());
        }
        queryWrapper.orderByDesc(RaSystem::getCreateDateTime);
        var raserverAreas= raSystemMapper.selectList(queryWrapper);
        var pageInfo = PageInfo.of(raserverAreas);
        return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse deleteRaSystems(String id, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaExtension>();
        wrapper.eq(RaExtension::getIsSoftDelete, false);
        wrapper.eq(RaExtension::getSystemId, id);
        if(raExtensionMapper.selectList(wrapper).size()>0){
           return RootResponse.error("请先删除本系统下的分机！！！");
        }
        // 与任务信息是否绑定关联
        var wrapper2 = new LambdaQueryWrapper<TaTask>();
        wrapper2.eq(TaTask::getIsSoftDelete, false);
        List<TaTask> taTasks = taTaskMapper.selectList(wrapper2);
        for (TaTask taTask : taTasks) {
            var wrapper3 = new LambdaQueryWrapper<RaTree>();
            wrapper3.eq(RaTree::getIsSoftDelete, false);
            wrapper3.eq(RaTree::getId, taTask.getTreeId());
            for (RaTree raTree : raTreeMapper.selectList(wrapper3)) {
                if (raTree.getSystemId().equals(id)){
                    return RootResponse.error("此系统涉及的雷达已被任务信息绑定！！！");
                }
            }
        }
        var raRadar=  new RaSystem();
        raRadar.setUpdater(userDTO.getUsername());
        raRadar.setUpdateDateTime(new Date());
        raRadar.setId(id);
        raRadar.setIsSoftDelete(true);
        int i =  raSystemMapper.updateById(raRadar);
        return i == 1 ? RootResponse.success(raSystemMapper.selectById(id)) : RootResponse.error();
    }

    @Override
    public RootResponse updateRaSystems(UpdateRaSystemsDTO updateRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaSystem>();
        wrapper.eq(RaSystem::getIsSoftDelete, false);
        wrapper.eq(RaSystem::getName,updateRaInterventionTypeDTO.getName());
        var raSystem=raSystemMapper.selectOne(wrapper);
        if(raSystem!=null&&!raSystem.getId().equals(updateRaInterventionTypeDTO.getId())){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
        }
        var raserverArea=new RaSystem();
        BeanCopyUtils.copy(updateRaInterventionTypeDTO,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =raSystemMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success(raSystemMapper.selectById(updateRaInterventionTypeDTO.getId())) : RootResponse.error();

    }

    @Override
    public RootResponse createRaExtensions(CreateRaExtensionsDTO createRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaExtension>();
        wrapper.eq(RaExtension::getIsSoftDelete, false);
        wrapper.eq(RaExtension::getName,createRaInterventionTypeDTO.getName());
        if(raExtensionMapper.selectOne(wrapper)!=null){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
        }
        var fiRadarType= new RaExtension();
        BeanCopyUtils.copy(createRaInterventionTypeDTO,fiRadarType);
        fiRadarType.setId(HandleUtils.UUID32());
        fiRadarType.setIsSoftDelete(false);
        fiRadarType.setCreator(userDTO.getUsername());
        fiRadarType.setCreateDateTime(new Date());
        int i =  raExtensionMapper.insert(fiRadarType);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectRaExtensions(RaExtensionsPageDTO raRadarsPageDTO) {
        var ids= new HashSet<String>();
        ids.add("0");
        if(raRadarsPageDTO.getRadarId()!=null){
            var queryWrapper1 = new LambdaQueryWrapper<RaTree>();
            queryWrapper1.eq(RaTree::getIsSoftDelete, false);
            queryWrapper1.isNotNull(RaTree::getExtensionId);
            queryWrapper1.eq(RaTree::getRadarId,raRadarsPageDTO.getRadarId());
            raTreeMapper.selectList(queryWrapper1).forEach(t->{
                ids.add(t.getExtensionId());
            });
            if(ids!=null&&ids.size()!=0){
                raRadarsPageDTO.setExtensionIds(ids);
            }
        }
        PageHelper.startPage(raRadarsPageDTO.getPageNum(), raRadarsPageDTO.getPageSize());
        var raserverAreas= raExtensionMapper.selectExtension(raRadarsPageDTO);
        var pageInfo = PageInfo.of(raserverAreas);
        return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse deleteRaExtensions(String id, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaKeyPartsModel>();
        wrapper.eq(RaKeyPartsModel::getIsSoftDelete, false);
        wrapper.eq(RaKeyPartsModel::getExtensionId, id);
        if(raKeyPartsModelMapper.selectList(wrapper).size()>0){
            return RootResponse.error("请先删除本分机下的关重件型号！！！");
        }
        // 与任务信息是否绑定关联
        var wrapper2 = new LambdaQueryWrapper<TaTask>();
        wrapper2.eq(TaTask::getIsSoftDelete, false);
        List<TaTask> taTasks = taTaskMapper.selectList(wrapper2);
        for (TaTask taTask : taTasks) {
            var wrapper3 = new LambdaQueryWrapper<RaTree>();
            wrapper3.eq(RaTree::getIsSoftDelete, false);
            wrapper3.eq(RaTree::getId, taTask.getTreeId());
            for (RaTree raTree : raTreeMapper.selectList(wrapper3)) {
                if (raTree.getExtensionId()!=null&&raTree.getExtensionId().equals(id)){
                    return  RootResponse.error("此分机涉及的雷达已被任务信息绑定！！！");
                }
            }
        }
        var raRadar=  new RaExtension();
        raRadar.setUpdater(userDTO.getUsername());
        raRadar.setUpdateDateTime(new Date());
        raRadar.setId(id);
        raRadar.setIsSoftDelete(true);
        int i =  raExtensionMapper.updateById(raRadar);
        return i == 1 ? RootResponse.success(raExtensionMapper.selectById(id)) : RootResponse.error();
    }

    @Override
    public RootResponse updateRaExtensions(UpdateRaExtensionsDTO updateRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaExtension>();
        wrapper.eq(RaExtension::getIsSoftDelete, false);
        wrapper.eq(RaExtension::getName,updateRaInterventionTypeDTO.getName());
       var raExtension= raExtensionMapper.selectOne(wrapper);
        if(raExtension!=null&&!raExtension.getId().equals(updateRaInterventionTypeDTO.getId())){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
        }
        var raserverArea=new RaExtension();
        BeanCopyUtils.copy(updateRaInterventionTypeDTO,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =raExtensionMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success(raExtensionMapper.selectById(updateRaInterventionTypeDTO.getId())) : RootResponse.error();

    }

    @Override
    public RootResponse createRaKeyPartsModels(CreateRaKeyPartsModelsDTO createRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaKeyPartsModel>();
        wrapper.eq(RaKeyPartsModel::getIsSoftDelete, false);
        wrapper.eq(RaKeyPartsModel::getName,createRaInterventionTypeDTO.getName());
        if(raKeyPartsModelMapper.selectOne(wrapper)!=null){
            return RootResponse.error("名称已存在！！！");
        }
        var fiRadarType= new RaKeyPartsModel();
        BeanCopyUtils.copy(createRaInterventionTypeDTO,fiRadarType);
        fiRadarType.setId(HandleUtils.UUID32());
        fiRadarType.setIsSoftDelete(false);
        fiRadarType.setCreator(userDTO.getUsername());
        fiRadarType.setCreateDateTime(new Date());
        int i =  raKeyPartsModelMapper.insert(fiRadarType);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectRaKeyPartsModels(RaKeyPartsModelsPageDTO raRadarsPageDTO) {
        var ids= new HashSet<String>();
        ids.add("0");
        if(raRadarsPageDTO.getRadarId()!=null){
            var queryWrapper1 = new LambdaQueryWrapper<RaTree>();
            queryWrapper1.eq(RaTree::getIsSoftDelete, false);
            queryWrapper1.isNotNull(RaTree::getKeyPartsModelId);
            queryWrapper1.eq(RaTree::getRadarId,raRadarsPageDTO.getRadarId());
            raTreeMapper.selectList(queryWrapper1).forEach(t->{
                ids.add(t.getKeyPartsModelId());
            });
                raRadarsPageDTO.setRaKeyPartsModelIds(ids);

        }
        PageHelper.startPage(raRadarsPageDTO.getPageNum(), raRadarsPageDTO.getPageSize());
        var raserverAreas= raKeyPartsModelMapper.selectRaKeyPartsModel(raRadarsPageDTO);
        var pageInfo = PageInfo.of(raserverAreas);
        return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse deleteRaKeyPartsModels(String id, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaKeyParts>();
        wrapper.eq(RaKeyParts::getIsSoftDelete, false);
        wrapper.eq(RaKeyParts::getModelId, id);
        if(raKeyPartsMapper.selectList(wrapper).size()>0){
            return RootResponse.error("此关重件型号已被使用，不能删除！！！");
        }
        // 与任务信息是否绑定关联
        if(taTaskMapper.selectIfInTask(id).size()>0){
            return RootResponse.error("此关重键涉及的雷达已被任务信息绑定！！！");

        }
        var raRadar=  new RaKeyPartsModel();
        raRadar.setUpdater(userDTO.getUsername());
        raRadar.setUpdateDateTime(new Date());
        raRadar.setId(id);
        raRadar.setIsSoftDelete(true);
        int i =  raKeyPartsModelMapper.updateById(raRadar);
        return i == 1 ? RootResponse.success(raKeyPartsModelMapper.selectById(id)) : RootResponse.error();
    }

    @Override
    public RootResponse updateRaKeyPartsModels(UpdateRaKeyPartsModelsDTO updateRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaKeyPartsModel>();
        wrapper.eq(RaKeyPartsModel::getIsSoftDelete, false);
        wrapper.eq(RaKeyPartsModel::getName,updateRaInterventionTypeDTO.getName());
        var raKeyPartsModel=raKeyPartsModelMapper.selectOne(wrapper);
        if(raKeyPartsModel!=null&&!raKeyPartsModel.getId().equals(updateRaInterventionTypeDTO.getId())){
            return RootResponse.error("名称已存在！！！");
        }
        var raserverArea=new RaKeyPartsModel();
        BeanCopyUtils.copy(updateRaInterventionTypeDTO,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =raKeyPartsModelMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success(raKeyPartsModelMapper.selectById(updateRaInterventionTypeDTO.getId())) : RootResponse.error();

    }

    @Override
    public RootResponse createRaStorages(CreateRaStoragesDTO createRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var fiRadarType= new RaStorage();
        BeanCopyUtils.copy(createRaInterventionTypeDTO,fiRadarType);
        fiRadarType.setId(HandleUtils.UUID32());
        fiRadarType.setIsSoftDelete(false);
        fiRadarType.setCreator(userDTO.getUsername());
        fiRadarType.setCreateDateTime(new Date());
        fiRadarType.setAreaId(userDTO.getAreaId());
        int i =  raStorageMapper.insert(fiRadarType);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectRaStorages(RaStoragesPageDTO raRadarsPageDTO) {
        PageHelper.startPage(raRadarsPageDTO.getPageNum(), raRadarsPageDTO.getPageSize());
        var raserverAreas= raStorageMapper.selectRaStorage(raRadarsPageDTO);
        var pageInfo = PageInfo.of(raserverAreas);
        pageInfo.getList().forEach(p->{
            String a=p.getKeyPartsModelTypeName()!=null?p.getKeyPartsModelTypeName():"";
            a+=p.getKeyPartsModelName()!=null?p.getKeyPartsModelName():"";
            p.setKeyPartsModelName(a);

        });
        return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse deleteRaStorages(String id, LoginUserDTO userDTO) {
//       var raStorage= raStorageMapper.selectById(id);
//       if(raStorage!=null){
//           var wrapper = new LambdaQueryWrapper<RaKeyPartsModel>();
//           wrapper.eq(RaKeyPartsModel::getIsSoftDelete, false);
//           wrapper.eq(RaKeyPartsModel::getId,raStorage.getKeyPartsModelId());
//           if(raKeyPartsModelMapper.selectList(wrapper).size()>0){
//               return RootResponse.error("请先删除型号信息！！！");
//           }
//       }
        var raRadar=  new RaStorage();
        raRadar.setUpdater(userDTO.getUsername());
        raRadar.setUpdateDateTime(new Date());
        raRadar.setId(id);
        raRadar.setIsSoftDelete(true);
        int i =  raStorageMapper.updateById(raRadar);
        return i == 1 ? RootResponse.success(raStorageMapper.selectById(id)) : RootResponse.error();
    }

    @Override
    public RootResponse updateRaStorages(UpdateRaStoragesDTO updateRaInterventionTypeDTO, LoginUserDTO userDTO) {
        var raserverArea=new RaStorage();
        BeanCopyUtils.copy(updateRaInterventionTypeDTO,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =raStorageMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success(raStorageMapper.selectById(updateRaInterventionTypeDTO.getId())) : RootResponse.error();

    }

    @Override
    public RootResponse selectRaTree(String radarId) {
        List<RaTreeVO> list= new ArrayList<>();
        List<RaTreeVO> list1= new ArrayList<>();
        List<RaTreeVO> list2= new ArrayList<>();
        List<RaTreeVO> list3= new ArrayList<>();
        var wrapper = new LambdaQueryWrapper<RaTree>();
        wrapper.eq(RaTree::getIsSoftDelete, false);
        wrapper.eq(RaTree::getRadarId,radarId);
        var raTrees=raTreeMapper.selectList(wrapper);
        raTrees.forEach(r->{
            var raTreeVO =new RaTreeVO();
            if(r.getSystemId()==null&&r.getExtensionId()==null&&r.getKeyPartsModelId()==null){
                var wrapper1 = new LambdaQueryWrapper<RaRadar>();
                wrapper1.eq(RaRadar::getIsSoftDelete, false);
                wrapper1.eq(RaRadar::getId,r.getRadarId());
                var raRadar=raRadarMapper.selectOne(wrapper1);
                if(raRadar!=null){
                    raTreeVO.setName( raRadar.getName());
                    raTreeVO.setTreeId(r.getId());
                    raTreeVO.setType(0);
                    raTreeVO.setId(radarId);
                    list.add(raTreeVO);
                }
            }
            if(r.getSystemId()!=null&&r.getKeyPartsModelId()==null&&r.getExtensionId()==null){
                var wrapper1 = new LambdaQueryWrapper<RaSystem>();
                wrapper1.eq(RaSystem::getIsSoftDelete, false);
                wrapper1.eq(RaSystem::getId,r.getSystemId());
               var raSystem= raSystemMapper.selectOne(wrapper1);
                if(raSystem!=null) {
                    raTreeVO.setName(raSystem.getName());
                    raTreeVO.setType(1);
                    raTreeVO.setTreeId(r.getId());
                    raTreeVO.setId(r.getSystemId());
                    raTreeVO.setPid(r.getRadarId());
                    list1.add(raTreeVO);
                }
            }
            if(r.getSystemId()!=null&&r.getKeyPartsModelId()==null&&r.getExtensionId()!=null){
                var wrapper1 = new LambdaQueryWrapper<RaExtension>();
                wrapper1.eq(RaExtension::getIsSoftDelete, false);
                wrapper1.eq(RaExtension::getId,r.getExtensionId());
               var raExtension= raExtensionMapper.selectOne(wrapper1);
               if(raExtension!=null){
                   raTreeVO.setName(raExtension.getName());
                   raTreeVO.setType(2);
                   raTreeVO.setTreeId(r.getId());
                   raTreeVO.setId(r.getExtensionId());
                   raTreeVO.setPid(r.getSystemId());
                   list2.add(raTreeVO);
               }
            }
            if(r.getSystemId()!=null&&r.getKeyPartsModelId()!=null&&r.getExtensionId()!=null){

                var wrapper1 = new LambdaQueryWrapper<RaKeyPartsModel>();
                wrapper1.eq(RaKeyPartsModel::getIsSoftDelete, false);
                wrapper1.eq(RaKeyPartsModel::getId,r.getKeyPartsModelId());
                var raKeyPartsModel=raKeyPartsModelMapper.selectOne(wrapper1);
                if(raKeyPartsModel!=null){
                    raTreeVO.setName(raKeyPartsTypeMapper.selectById(raKeyPartsModel.getTypeId()).getName()+raKeyPartsModel.getName());
                    raTreeVO.setType(3);
                    raTreeVO.setTreeId(r.getId());
                    raTreeVO.setPid(r.getExtensionId());
                    raTreeVO.setId(r.getKeyPartsModelId());
                    list3.add(raTreeVO);
                }
            }
        });
                list.forEach(l->{
                    l.setChildren(list1);
        });
        list1.forEach(l->{
            var raTreeVOs=new ArrayList<RaTreeVO>();
            list2.forEach(l1->{
                if(l1.getPid().equals(l.getId())){
                    raTreeVOs.add(l1);
                }
                if(!raTreeVOs.isEmpty()){
                    l.setChildren(raTreeVOs);
                }
            });

        });
        list2.forEach(l->{
            var raTreeVOs=new ArrayList<RaTreeVO>();
            list3.forEach(l1->{
                if(l1.getPid().equals(l.getId())){
                    raTreeVOs.add(l1);
                }
                if(!raTreeVOs.isEmpty()){
                    l.setChildren(raTreeVOs);
                }
            });

        });
        return RootResponse.success(list);
    }
    //递归查子菜单

    @Override
    public RootResponse selectRaTreeNoDo() {
        List<RaTreeVO> raTreeVOS = new ArrayList<>();
        var wrapper = new LambdaQueryWrapper<RaKeyPartsModel>();
        wrapper.eq(RaKeyPartsModel::getIsSoftDelete, false);
       var raKeyPartsModels=raKeyPartsModelMapper.selectList(wrapper);
        var wrapper1 = new LambdaQueryWrapper<RaSystem>();
        wrapper1.eq(RaSystem::getIsSoftDelete, false);
        var raSystems=raSystemMapper.selectList(wrapper1);
        var wrapper2 = new LambdaQueryWrapper<RaExtension>();
        wrapper2.eq(RaExtension::getIsSoftDelete, false);
        var raExtensions=raExtensionMapper.selectList(wrapper2);
        for(RaSystem r:raSystems){
            var raTreeVO =new RaTreeVO();
            raTreeVO.setName(r.getName());
            raTreeVO.setType(1);
            raTreeVO.setId(r.getId());
            raExtensions.forEach(e->{
                if(r.getId().equals(e.getSystemId())){
                    var raTreeVO1 =new RaTreeVO();
                    raTreeVO1.setName(e.getName());
                    raTreeVO1.setType(2);
                    raTreeVO1.setId(e.getId());
                    raTreeVO1.setPid(e.getSystemId());
                    raKeyPartsModels.forEach(p->{
                        if(e.getId().equals(p.getExtensionId())){
                            var raTreeVO2 =new RaTreeVO();
                            raTreeVO2.setName(raKeyPartsTypeMapper.selectById(p.getTypeId()).getName()+p.getName());
                            raTreeVO2.setType(3);
                            raTreeVO2.setId(p.getId());
                            raTreeVO2.setPid(p.getExtensionId());
                            if(raTreeVO1.getChildren()!=null){
                                raTreeVO1.getChildren().add(raTreeVO2);
                            }else {
                                List<RaTreeVO> raTreeVOS2 = new ArrayList<>();
                                raTreeVOS2.add(raTreeVO2);
                                raTreeVO1.setChildren(raTreeVOS2);
                            }
                        }
                    });
                    if(raTreeVO.getChildren()!=null){
                        raTreeVO.getChildren().add(raTreeVO1);
                    }else {
                        List<RaTreeVO> raTreeVOS1 = new ArrayList<>();
                        raTreeVOS1.add(raTreeVO1);
                        raTreeVO.setChildren(raTreeVOS1);
                    }

                }
            });
            raTreeVOS.add(raTreeVO);
        }

        return RootResponse.success(raTreeVOS);
    }

    @Override
    public RootResponse createRaTrees(CreateRaTreesDTO createRaTreesDTO,LoginUserDTO userDTO) {
//       String radarId=createRaTreesDTO.getIds().get(0).split("-")[0];
//        var wrapper = new LambdaQueryWrapper<RaTree>();
//        wrapper.eq(RaTree::getRadarId, radarId);
//        raTreeMapper.delete(wrapper);
        for( var s:createRaTreesDTO.getDeleteIds()){
            String[] ids=s.split("-");
            if(ids.length==1){
                var wrapper = new LambdaQueryWrapper<RaTree>();
                wrapper.eq(RaTree::getRadarId, ids[0]);
                wrapper.eq(RaTree::getIsSoftDelete,false);
                var raTree =raTreeMapper.selectOne(wrapper);
                var wrapper1 = new LambdaQueryWrapper<TaTask>();
                wrapper1.eq(TaTask::getTreeId, raTree.getId());
                wrapper1.eq(TaTask::getIsSoftDelete,false);
                var taTask =taTaskMapper.selectList(wrapper1);
                if(taTask.size()>0){
                    return RootResponse.error("修改部位已被任务信息绑定！！！");
                }
                 raTreeMapper.delete(wrapper);
            }
            else if(ids.length==2){
                var wrapper = new LambdaQueryWrapper<RaTree>();
                wrapper.eq(RaTree::getRadarId, ids[0]);
                wrapper.eq(RaTree::getSystemId, ids[1]);
                wrapper.eq(RaTree::getIsSoftDelete,false);
                var raTree =raTreeMapper.selectOne(wrapper);
                var wrapper1 = new LambdaQueryWrapper<TaTask>();
                wrapper1.eq(TaTask::getTreeId, raTree.getId());
                wrapper1.eq(TaTask::getIsSoftDelete,false);
                var taTask =taTaskMapper.selectList(wrapper1);
                if(taTask.size()>0){
                    return RootResponse.error("修改部位已被任务信息绑定！！！");
                }
                raTreeMapper.delete(wrapper);
            }  else if(ids.length==3){
                var wrapper = new LambdaQueryWrapper<RaTree>();
                wrapper.eq(RaTree::getRadarId, ids[0]);
                wrapper.eq(RaTree::getSystemId, ids[1]);
                wrapper.eq(RaTree::getExtensionId, ids[2]);
                wrapper.eq(RaTree::getIsSoftDelete,false);
                var raTree =raTreeMapper.selectOne(wrapper);
                var wrapper1 = new LambdaQueryWrapper<TaTask>();
                wrapper1.eq(TaTask::getTreeId, raTree.getId());
                wrapper1.eq(TaTask::getIsSoftDelete,false);
                var taTask =taTaskMapper.selectList(wrapper1);
                if(taTask.size()>0){
                    return RootResponse.error("修改部位已被任务信息绑定！！！");
                }
                raTreeMapper.delete(wrapper);
            }
            else if(ids.length==4){
                var wrapper = new LambdaQueryWrapper<RaTree>();
                wrapper.eq(RaTree::getRadarId, ids[0]);
                wrapper.eq(RaTree::getSystemId, ids[1]);
                wrapper.eq(RaTree::getExtensionId, ids[2]);
                wrapper.eq(RaTree::getKeyPartsModelId, ids[3]);
                wrapper.eq(RaTree::getIsSoftDelete,false);
                var raTree =raTreeMapper.selectOne(wrapper);
                var wrapper1 = new LambdaQueryWrapper<TaTask>();
                wrapper1.eq(TaTask::getTreeId, raTree.getId());
                wrapper1.eq(TaTask::getIsSoftDelete,false);
                var taTask =taTaskMapper.selectList(wrapper1);
                if(taTask.size()>0){
                    return RootResponse.error("修改部位已被任务信息绑定！！！");
                }
                raTreeMapper.delete(wrapper);
            }
        }

        createRaTreesDTO.getCreateIds().forEach(s->{
            String[] ids=s.split("-");
            if(ids.length==1){
                var raTree1=new RaTree();
                raTree1.setId(HandleUtils.UUID32());
                raTree1.setCreator(userDTO.getUsername());
                raTree1.setCreateDateTime(new Date());
                raTree1.setIsSoftDelete(false);
                raTree1.setRadarId(ids[0]);
                raTreeMapper.insert(raTree1);
            }
            if(ids.length==2){
                var raTree=new RaTree();
                raTree.setId(HandleUtils.UUID32());
                raTree.setCreator(userDTO.getUsername());
                raTree.setCreateDateTime(new Date());
                raTree.setIsSoftDelete(false);
                raTree.setRadarId(ids[0]);
                raTree.setSystemId(ids[1]);
                raTreeMapper.insert(raTree);
            }  if(ids.length==3){
                var raTree=new RaTree();
                raTree.setId(HandleUtils.UUID32());
                raTree.setCreator(userDTO.getUsername());
                raTree.setCreateDateTime(new Date());
                raTree.setIsSoftDelete(false);
                raTree.setRadarId(ids[0]);
                raTree.setSystemId(ids[1]);
                raTree.setExtensionId(ids[2]);
                raTreeMapper.insert(raTree);
            }
            if(ids.length==4){
                var raTree=new RaTree();
                raTree.setId(HandleUtils.UUID32());
                raTree.setCreator(userDTO.getUsername());
                raTree.setCreateDateTime(new Date());
                raTree.setIsSoftDelete(false);
                raTree.setRadarId(ids[0]);
                raTree.setSystemId(ids[1]);
                raTree.setExtensionId(ids[2]);
                raTree.setKeyPartsModelId(ids[3]);
                raTreeMapper.insert(raTree);
            }

        });

        return RootResponse.success("添加成功");
    }

    @Override
    public RootResponse selectRadarIdAndModelId(Integer pageNum,Integer pageSize,String radarId, String modelId) {
        if(pageNum!=null&&pageSize!=null){
            PageHelper.startPage(pageNum, pageSize);
        }else {
            PageHelper.startPage(1, 10);
        }
        var wrapper = new LambdaQueryWrapper<RaKeyParts>();
        wrapper.eq(RaKeyParts::getIsSoftDelete,false);
        wrapper.eq(RaKeyParts::getRadarId,radarId);
        wrapper.eq(RaKeyParts::getModelId,modelId);
        wrapper.orderByDesc(RaKeyParts::getCreateDateTime);
        var raserverAreas= raKeyPartsMapper.selectList(wrapper);
        var pageInfo = PageInfo.of(raserverAreas);
        return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse createKeyPartsSwitching(CreateKeyPartsSwitchingDTO createKeyPartsSwitchingDTO,LoginUserDTO userDTO) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        var wrapper = new LambdaQueryWrapper<RaStorage>();
        wrapper.eq(RaStorage::getId,createKeyPartsSwitchingDTO.getRaStoragesId());
        wrapper.eq(RaStorage::getIsSoftDelete,false);
       var raStorage= raStorageMapper.selectOne(wrapper);
       if(raStorage.getStorageNum()==0){
           return  RootResponse.error("库存不足");
       }
        if(raStorage!=null&&raStorage.getStorageNum()!=0){
            raStorage.setStorageNum(raStorage.getStorageNum()-1);
        }
        raStorageMapper.updateById(raStorage);
        var wrapper1 = new LambdaQueryWrapper<RaKeyParts>();
        wrapper1.eq(RaKeyParts::getRadarId,createKeyPartsSwitchingDTO.getRadarId());
        wrapper1.eq(RaKeyParts::getModelId,createKeyPartsSwitchingDTO.getKeyPartsModelId());
        var raKeyParts= new RaKeyParts();
        raKeyParts.setIsUse(0);
        raKeyPartsMapper.update(raKeyParts,wrapper1);
        var raKeyParts1= new RaKeyParts();
        raKeyParts1.setId(HandleUtils.UUID32());
        raKeyParts1.setIsUse(1);
        raKeyParts1.setIsSoftDelete(false);
        raKeyParts1.setCreator(userDTO.getUsername());
        raKeyParts1.setCreateDateTime(new Date());
        raKeyParts1.setModelId(createKeyPartsSwitchingDTO.getKeyPartsModelId());
        raKeyParts1.setRadarId(createKeyPartsSwitchingDTO.getRadarId());
        try { raKeyParts1.setStartUseTime(simpleDateFormat.parse(createKeyPartsSwitchingDTO.getStartTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        raKeyPartsMapper.insert(raKeyParts1);
        //根据保障基础信息和之前使用的关重件id去停止保障计划
//        var wrapper2 = new LambdaQueryWrapper<FaEnsure>();
//        wrapper2.eq(FaEnsure::getIsSoftDelete,false);
//        wrapper2.eq(FaEnsure::getKeyPartsModelId,createKeyPartsSwitchingDTO.getKeyPartsModelId());
//        var faEnsureIds=new ArrayList<String>();
//        faEnsureMapper.selectList(wrapper2).forEach(f->{
//            faEnsureIds.add(f.getId());
//        });
//        if(faEnsureIds.size()!=0){
//            var wrapper3 = new LambdaQueryWrapper<FaEnsurePlan>();
//            wrapper3.eq(FaEnsurePlan::getIsSoftDelete,false);
//            wrapper3.in(FaEnsurePlan::getEnsureId,faEnsureIds);
//            var faEnsurePlan=new FaEnsurePlan();
//            faEnsurePlan.setStatus(0);
//            faEnsurePlanMapper.update(faEnsurePlan,wrapper3);
//        }
//        //根据保障基础信息和新增的关重件id 新增对应的保障计划，有几个保障基础信息，就新增几条
//        var wrapper4 = new LambdaQueryWrapper<RaKeyParts>();
//        wrapper4.eq(RaKeyParts::getIsSoftDelete,false);
//        wrapper4.eq(RaKeyParts::getModelId,createKeyPartsSwitchingDTO.getKeyPartsModelId());
//        wrapper4.eq(RaKeyParts::getRadarId,createKeyPartsSwitchingDTO.getRadarId());
//       var raKeyParts2= raKeyPartsMapper.selectList(wrapper4);
//       String raKeyPartsId=null;
//       if(raKeyParts2!=null&&raKeyParts2.size()!=0){
//           raKeyPartsId=raKeyParts2.get(0).getId();
//       }
//       for(String faEnsureId:faEnsureIds){
//           if(raKeyPartsId!=null){
//               var faEnsurePlan1= new FaEnsurePlan();
//               faEnsurePlan1.setId(HandleUtils.UUID32());
//               faEnsurePlan1.setCreator(userDTO.getUsername());
//               faEnsurePlan1.setCreateDateTime(new Date());
//               faEnsurePlan1.setStatus(1);
//               faEnsurePlan1.setEnsureId(faEnsureId);
//               faEnsurePlan1.setKeyPartsId(raKeyPartsId);
//               faEnsurePlanMapper.insert(faEnsurePlan1);
//           }
//       }
        return  RootResponse.success(raStorage);
    }

    @Override
    public RootResponse RaStationMaps() {
      var raRadarMapVOS=  raRadarMapper.selectRaRadarMap();
       return RootResponse.success(raRadarMapVOS);
    }

    @Override
    public RootResponse RaStationMapsStatus() {
       var raRadarMapStatusVOS= raRadarMapper.selectRaRadarMapStatus();

        raRadarMapStatusVOS.forEach(r->{
           var map= timedTaskFaultResultMapper.selectTimedTaskFaultResultNum(r.getId());
           if(map!=null&&map.get("non_switch_fault")!=null&&map.get("switch_fault")!=null&&map.get("non_switch_low")!=null){
               if(Integer.valueOf(map.get("non_switch_fault").toString())!=0||Integer.valueOf(map.get("switch_fault").toString())!=0){
                   r.setStatus(2);
               }else if(Integer.valueOf(map.get("non_switch_low").toString())!=0){
                   r.setStatus(1);
               }else {
                   r.setStatus(0);
               }
           }else {
               r.setStatus(3);
           }
            var wrapper = new LambdaQueryWrapper<RaInterventionType>();
            wrapper.eq(RaInterventionType::getRadarId,r.getId());
            wrapper.eq(RaInterventionType::getIsSoftDelete,0);
            wrapper.eq(RaInterventionType::getIsIntervention,1);
            if(raInterventionTypeMapper.selectList(wrapper).size()==0){
                r.setStatus(4);
            }
        });
        return RootResponse.success(raRadarMapStatusVOS);
    }

    @Override
    public RootResponse selectRaRadarsById(RaRadarsPageDTO serverAreaPageDTO) {
        return RootResponse.success( raRadarMapper.selectRaRadarById(serverAreaPageDTO.getRadarId()));
    }

//    @Override
//    public RootResponse createRaKeyParts(CreateRaKeyPartsDTO createRaInterventionTypeDTO, LoginUserDTO userDTO) {
//        var wrapper = new LambdaQueryWrapper<RaKeyParts>();
//        wrapper.eq(RaKeyParts::getIsSoftDelete, false);
//        wrapper.eq(RaKeyParts::getName,createRaInterventionTypeDTO.getName());
//        if(raKeyPartsMapper.selectOne(wrapper)!=null){
//            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
//        }
//        var fiRadarType= new RaKeyPartsModel();
//        BeanCopyUtils.copy(createRaInterventionTypeDTO,fiRadarType);
//        fiRadarType.setId(HandleUtils.UUID32());
//        fiRadarType.setIsSoftDelete(false);
//        fiRadarType.setCreator(userDTO.getUsername());
//        fiRadarType.setCreateDateTime(new Date());
//        int i =  raKeyPartsMapper.insert(fiRadarType);
//        return i == 1 ? RootResponse.success() : RootResponse.error();
//    }
//
//    @Override
//    public RootResponse selectRaKeyParts(RaKeyPartsPageDTO raRadarsPageDTO) {
//        return null;
//    }
//
//    @Override
//    public RootResponse deleteRaKeyParts(String id, LoginUserDTO userDTO) {
//        return null;
//    }
//
//    @Override
//    public RootResponse updateRaKeyParts(UpdateRaKeyPartsDTO updateRaInterventionTypeDTO, LoginUserDTO userDTO) {
//        return null;
//    }

    @Override
    public RootResponse createFiRadarTypes(CreateFiRadarTypesDTO createServerAreaDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<FiRadarType>();
        wrapper.eq(FiRadarType::getIsSoftDelete, false);
        wrapper.and(wrapper1 -> wrapper1.eq(FiRadarType::getName,createServerAreaDTO.getName()).or().eq(FiRadarType::getSign, createServerAreaDTO.getSign()));
        if(fiRadarTypeMapper.selectList(wrapper).size()!=0){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称或标志已存在！！！");
        }
        var fiRadarType= new FiRadarType();
        BeanCopyUtils.copy(createServerAreaDTO,fiRadarType);
        fiRadarType.setId(HandleUtils.UUID32());
        fiRadarType.setIsSoftDelete(false);
        fiRadarType.setCreator(userDTO.getUsername());
        fiRadarType.setCreateDateTime(new Date());
        int i =  fiRadarTypeMapper.insert(fiRadarType);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectFiRadarTypes(FiRadarTypesPageDTO serverAreaPageDTO) {
        PageHelper.startPage(serverAreaPageDTO.getPageNum(), serverAreaPageDTO.getPageSize());
        var queryWrapper = new LambdaQueryWrapper<FiRadarType>();
        queryWrapper.eq(FiRadarType::getIsSoftDelete, false);
        if(serverAreaPageDTO.getName()!=null){
            queryWrapper.like(FiRadarType::getName, serverAreaPageDTO.getName());
        }
        queryWrapper.orderByDesc(FiRadarType::getCreateDateTime);
        var raserverAreas= fiRadarTypeMapper.selectList(queryWrapper);
        var pageInfo = PageInfo.of(raserverAreas);

        return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse deleteFiRadarTypes(String id, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<FiFileType>();
        wrapper.eq(FiFileType::getIsSoftDelete, false);
        wrapper.eq(FiFileType::getRadarTypeId, id);
        if(fiFileTypeMapper.selectList(wrapper).size()>0){
            return     RootResponse.error("请先删除此雷达类型下的文件类型！！！");
        }
        var wrapper1 = new LambdaQueryWrapper<RaRadar>();
        wrapper1.eq(RaRadar::getIsSoftDelete, false);
        wrapper1.eq(RaRadar::getTypeId, id);
        if(raRadarMapper.selectList(wrapper1).size()>0){
            return     RootResponse.error("请先删除此雷达类型下的雷达信息！！！");
        }
        var raServerType=  new FiRadarType();
        raServerType.setUpdater(userDTO.getUsername());
        raServerType.setUpdateDateTime(new Date());
        raServerType.setId(id);
        raServerType.setIsSoftDelete(true);
        int i =  fiRadarTypeMapper.updateById(raServerType);
        return i == 1 ? RootResponse.success(fiRadarTypeMapper.selectById(id)) : RootResponse.error();
    }

    @Override
    public RootResponse updateFiRadarTypes(UpdateFiRardarTypesDTO updateServerArea, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<FiRadarType>();
        wrapper.eq(FiRadarType::getIsSoftDelete, false);
        wrapper.and(wrapper1 -> wrapper1.eq(FiRadarType::getName,updateServerArea.getName()).or().eq(FiRadarType::getSign, updateServerArea.getSign()));
        var fiRadarType=fiRadarTypeMapper.selectList(wrapper);
        if(fiRadarType.size()>1){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称或标志已存在！！！");
        }
        if(fiRadarType.size()==1&&!fiRadarType.get(0).getId().equals(updateServerArea.getId())){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称或标志已存在！！！");
        }
        var raserverArea=new FiRadarType();
        BeanCopyUtils.copy(updateServerArea,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =fiRadarTypeMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse createRaKeyPartsType(CreateRaKeyPartsTypeDTO createServerAreaDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaKeyPartsType>();
        wrapper.eq(RaKeyPartsType::getIsSoftDelete, false);
        wrapper.eq(RaKeyPartsType::getName,createServerAreaDTO.getName());
        if(raKeyPartsTypeMapper.selectOne(wrapper)!=null){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
        }
        var fiRadarType= new RaKeyPartsType();
        BeanCopyUtils.copy(createServerAreaDTO,fiRadarType);
        fiRadarType.setId(HandleUtils.UUID32());
        fiRadarType.setIsSoftDelete(false);
        fiRadarType.setCreator(userDTO.getUsername());
        fiRadarType.setCreateDateTime(new Date());
        int i =  raKeyPartsTypeMapper.insert(fiRadarType);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectRaKeyPartsType(RaKeyPartsTypePageDTO serverAreaPageDTO) {
        PageHelper.startPage(serverAreaPageDTO.getPageNum(), serverAreaPageDTO.getPageSize());
        var queryWrapper = new LambdaQueryWrapper<RaKeyPartsType>();
        queryWrapper.eq(RaKeyPartsType::getIsSoftDelete, false);
        if(serverAreaPageDTO.getName()!=null){
            queryWrapper.like(RaKeyPartsType::getName, serverAreaPageDTO.getName());
        }
        queryWrapper.orderByDesc(RaKeyPartsType::getCreateDateTime);
        var raserverAreas= raKeyPartsTypeMapper.selectList(queryWrapper);
        var pageInfo = PageInfo.of(raserverAreas);

        return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse deleteRaKeyPartsType(String id, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaKeyPartsModel>();
        wrapper.eq(RaKeyPartsModel::getIsSoftDelete, false);
        wrapper.eq(RaKeyPartsModel::getTypeId,id);
        if(raKeyPartsModelMapper.selectList(wrapper).size()>0){
            return RootResponse.error("请先删除关重件型号！！！");
        }
        var raServerType=  new RaKeyPartsType();
        raServerType.setUpdater(userDTO.getUsername());
        raServerType.setUpdateDateTime(new Date());
        raServerType.setId(id);
        raServerType.setIsSoftDelete(true);
        int i =  raKeyPartsTypeMapper.updateById(raServerType);
        return i == 1 ? RootResponse.success(raKeyPartsTypeMapper.selectById(id)) : RootResponse.error();
    }

    @Override
    public RootResponse updateRaKeyPartsType(UpdateRaKeyPartsTypeDTO updateServerArea, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaKeyPartsType>();
        wrapper.eq(RaKeyPartsType::getIsSoftDelete, false);
        wrapper.eq(RaKeyPartsType::getName,updateServerArea.getName());
        var raKeyPartsType=raKeyPartsTypeMapper.selectOne(wrapper);
        if(raKeyPartsType!=null&&!raKeyPartsType.getId().equals(updateServerArea.getId())){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
        }
        var raserverArea=new RaKeyPartsType();
        BeanCopyUtils.copy(updateServerArea,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =raKeyPartsTypeMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success(raKeyPartsTypeMapper.selectById(updateServerArea.getId())) : RootResponse.error();
    }

    @Override
    public RootResponse createRaStorageHomes(CreateRaStorageHomesDTO createServerAreaDTO, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaStorageHome>();
        wrapper.eq(RaStorageHome::getIsSoftDelete, false);
        wrapper.eq(RaStorageHome::getName,createServerAreaDTO.getName());
        wrapper.eq(RaStorageHome::getAreaId,userDTO.getAreaId());
        if(raStorageHomeMapper.selectOne(wrapper)!=null){
            return RootResponse.error("名称已存在！！！");
        }
        var fiRadarType= new RaStorageHome();
        BeanCopyUtils.copy(createServerAreaDTO,fiRadarType);
        fiRadarType.setId(HandleUtils.UUID32());
        fiRadarType.setIsSoftDelete(false);
        fiRadarType.setCreator(userDTO.getUsername());
        fiRadarType.setCreateDateTime(new Date());
        fiRadarType.setAreaId(userDTO.getAreaId());
        int i =  raStorageHomeMapper.insert(fiRadarType);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }

    @Override
    public RootResponse selectRaStorageHomes(RaStorageHomesPageDTO serverAreaPageDTO) {
        PageHelper.startPage(serverAreaPageDTO.getPageNum(), serverAreaPageDTO.getPageSize());
        var queryWrapper = new LambdaQueryWrapper<RaStorageHome>();
        queryWrapper.eq(RaStorageHome::getIsSoftDelete, false);
        if(serverAreaPageDTO.getName()!=null){
            queryWrapper.like(RaStorageHome::getName, serverAreaPageDTO.getName());
        }
        queryWrapper.eq(RaStorageHome::getAreaId,serverAreaPageDTO.getAreaId());
        var raserverAreas= raStorageHomeMapper.selectList(queryWrapper);
        var pageInfo = PageInfo.of(raserverAreas);

        return RootResponse.success(PageResult.page(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse deleteRaStorageHomes(String id, LoginUserDTO userDTO) {
        var queryWrapper = new LambdaQueryWrapper<RaStorage>();
        queryWrapper.eq(RaStorage::getIsSoftDelete,false);
        queryWrapper.eq(RaStorage::getStorageHomeId,id);
        if(raStorageMapper.selectList(queryWrapper).size()>0){
            return RootResponse.error("请先删除库存！！！");
        }
        var raServerType=  new RaStorageHome();
        raServerType.setUpdater(userDTO.getUsername());
        raServerType.setUpdateDateTime(new Date());
        raServerType.setId(id);
        raServerType.setIsSoftDelete(true);
        int i =  raStorageHomeMapper.updateById(raServerType);
        return i == 1 ? RootResponse.success(raStorageHomeMapper.selectById(id)) : RootResponse.error();
    }

    @Override
    public RootResponse updateRaStorageHomes(UpdateRaStorageHomesDTO updateServerArea, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<RaStorageHome>();
        wrapper.eq(RaStorageHome::getIsSoftDelete, false);
        wrapper.eq(RaStorageHome::getName,updateServerArea.getName());
        // add the area
        wrapper.eq(RaStorageHome::getAreaId,userDTO.getAreaId());
       var raStorageHome= raStorageHomeMapper.selectOne(wrapper);
        if(raStorageHome!=null&&!raStorageHome.getId().equals(updateServerArea.getId())){
            return RootResponse.error(CodeStatusEnum.EXIST_USERNAME.getCode(),"名称已存在！！！");
        }
        var raserverArea=new RaStorageHome();
        BeanCopyUtils.copy(updateServerArea,raserverArea);
        raserverArea.setUpdater(userDTO.getUsername());
        raserverArea.setUpdateDateTime(new Date());
        int i =raStorageHomeMapper.updateById(raserverArea);
        return i == 1 ? RootResponse.success() : RootResponse.error();
    }
}
